 #include <stdio.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <stdlib.h>
#include <vector>
typedef void (*hander_t)();

std::vector<hander_t> handlers;

void fun_one()
{
    printf("this is temples task_one...\n");
};

void fun_two()
{
    printf("this is temples task_two...\n");
};
void Load()
{
    handlers.push_back(fun_one);
    handlers.push_back(fun_two);
};
void handler()
{
    if(handlers.empty())
    {
        Load();
    }
    for(auto iter:handlers)
    {
        iter();
    }
};


int main()
{
    pid_t pid;
    pid=fork();
    if(pid<0)
    {
         printf("%s fork error\n",__FUNCTION__);
         return 1;
    } 
    else if(pid==0)
    {
        printf("child is run,pid is :%d\n",getpid());
        sleep(5);
        exit(252);
    }
    else 
    {
        int status=0;
        pid_t ret=0;
        do{  
         ret=waitpid(-1,&status,WNOHANG);
        // 非阻塞式等待，等待失败返回0，等待成功返回子进程pid
         if(ret==0)
         {
              printf("child is running\n");
          }
         handler();
        }while(ret==0);
        if(WIFEXITED(status) && ret==pid)
        {
            printf("wait child 5s succes,child return code is: %d.\n", WEXITSTATUS(status));
        }
        else 
        {
            printf("wait child failed, return %d .\n",WTERMSIG(status));
            return 1;
        }
    }
    
    return 0;
}
